package com.haiyou.data.common.persister.mysql.persister;

import java.util.Set;

import org.springframework.stereotype.Component;

import com.google.common.collect.Sets;
import com.google.common.eventbus.Subscribe;
import com.haiyou.common.event.asyc.AbstractAsyncEventObserver;
import com.haiyou.common.spring.event.SpringApplicationContextReadyEvent;
import com.haiyou.data.common.persister.mysql.help.TableHelper;
import com.haiyou.data.common.persister.mysql.meta.TableMeta;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class PersisterObserver extends AbstractAsyncEventObserver {

	@Subscribe
	public void recv(SpringApplicationContextReadyEvent event) {
		// TODO 检查mysql表命名重复
		Set<String> nameSet = Sets.newHashSet();
		for (TableMeta tableMeta : TableHelper.tableMetaPool.values()) {
			String tab = tableMeta.getSimpleName();
			if (!nameSet.add(tab)) {
				log.error(String.format("%s数据表%s命名冲突", tableMeta.getEntityClassName(), tab));
				System.exit(-1);
			}
		}
	}

}
